package android.itv.controller;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.itv.R;
import android.itv.library.Database;
import android.itv.library.JsonPrase;
import android.itv.library.ServiceHelper;
import android.itv.library.Utils;
import android.itv.model.BroadcastSchedule;
import android.itv.model.IBroadcastSchedule;
import android.itv.model.Notification;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;

public class BroadcastScheduleController implements IBroadcastSchedule
{
    private ListView listView;
    private Context context;
    private TextView txtTitle;
    CustomeAdapterForListSchedule adapter;

    public BroadcastScheduleController()
    {
    }
    public BroadcastScheduleController(ListView listView,Context context,TextView txtTitle,CustomeAdapterForListSchedule cafs)
    {
	this.listView=listView;
	this.context=context;
	this.txtTitle=txtTitle;
	this.adapter=cafs;
    }

    @Override
    public void loadBroadcastSchedulesFromServiceToView(String idChannel, String date,final Context context)
    {
	BroadcastSchedule bs=new  BroadcastSchedule();
	String schedule=new ServiceHelper(context).getSchedule(idChannel, date);
	bs.setDate(date);
	bs.setIdChannel(idChannel);
	bs.setListprograme(JsonPrase.parseSchedule(schedule));
	
	CustomeAdapterForListSchedule adapter=new CustomeAdapterForListSchedule(context, bs);
	listView.setAdapter(adapter);
    }
    
    @Override
    public void saveBroadcastScheduleToDB(BroadcastSchedule bs)
    {
	
    }


    @Override
    public void loadBroadcastSchedulesFromDBToView(final BroadcastSchedule br,final Context context)
    {
	 this.adapter=new CustomeAdapterForListSchedule(context, br);

	 //thay doi tieu de
	 Database db=new Database(context);
	 this.txtTitle.setText(db.getNameChannel(adapter.broadSchedule.getIdChannel()));
	 //

	 listView.setAdapter(adapter);
	 listView.setSelection(Utils.getProgramePlayedNow(br)); 
	 listView.setOnItemClickListener(new OnItemClickListener()
	{
	     @Override
	    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	    {
		int hour, minute;
		String content="";
		String time=br.getListprograme().get(arg2).getTime();
		String nameChannel="";
		Database db=new Database(context);
		nameChannel=db.getNameChannel(br.getIdChannel());
		hour=Utils.getHour(time);
		minute=Utils.getMinute(time);
		content=nameChannel+"-"+time+"-"+br.getListprograme().get(arg2).getTitle()+"\n"+br.getListprograme().get(arg2).getDescription();
		createDialog(hour,minute,content,nameChannel);
	    }
	});
    }
    
    public void removeSchedulteInView()
    {
	this.adapter.removeBroacastSchedule();
	this.adapter.notifyDataSetChanged();
    }
    
    public class CustomeAdapterForListSchedule extends BaseAdapter
    {
	public BroadcastSchedule broadSchedule;
	private Context context;
	private LayoutInflater inflater;
	
	public CustomeAdapterForListSchedule(Context context,BroadcastSchedule broadSchedule)
        {
	    this.context=context;
	    this.broadSchedule=broadSchedule;
	    inflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
	
	@Override
        public int getCount()
        {
	   return broadSchedule.getListprograme().size();
        }

	@Override
        public Object getItem(int arg0)
        {
	    return broadSchedule.getListprograme().get(arg0);
        }

	@Override
        public long getItemId(int arg0)
        {
	    return arg0;
        }

	@Override
        public View getView(int arg0, View arg1, ViewGroup arg2)
        {
	    View view=arg1;
	    if(view==null)
	    {
		view=inflater.inflate(R.layout.customeadapterforlistschedule, null);
	    }
	    TextView txttime=(TextView)view.findViewById(R.id.txttime);
	    TextView txttitleprograme=(TextView)view.findViewById(R.id.txttitileprogram);
	    TextView txtcontentprogram=(TextView)view.findViewById(R.id.txtcontentprogram);	    
	    
	    txttime.setText(broadSchedule.getListprograme().get(arg0).getTime());
	    txttitleprograme.setText(broadSchedule.getListprograme().get(arg0).getTitle());
	    txtcontentprogram.setText(broadSchedule.getListprograme().get(arg0).getDescription());
	    return view;
        }
	
	public void removeBroacastSchedule()
	{
	    if(broadSchedule.getListprograme().size()>0)
	    {
		broadSchedule.getListprograme().clear();
	    }
	}
	
    }
    
    
    private void createDialog(int hour,int minute,final String content,final String nameChannel)
    {
	AlertDialog.Builder builder = new AlertDialog.Builder(context);
	AlertDialog dialog;
	final TimePicker tp;
	final EditText edtContentNotify;
	
	LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	View view=inflater.inflate(R.layout.dialogsettime, null);
	tp=(TimePicker)view.findViewById(R.id.timePicker);
	edtContentNotify=(EditText)view.findViewById(R.id.edtcontentNofify);
	
	builder.setView(view);	
	tp.setIs24HourView(true);
	tp.setCurrentHour(hour);
	tp.setCurrentMinute(minute);
	edtContentNotify.setText(content);
	
	builder.setTitle(R.string.timetonotification);
	builder.setIcon(android.R.drawable.ic_dialog_info);
	builder.setPositiveButton(R.string.btnpositive, new OnClickListener()
	{
	    
	    @Override
	    public void onClick(DialogInterface dialog, int which)
	    {		
		int hour=tp.getCurrentHour();
		int minute=tp.getCurrentMinute();
		Calendar cal=Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE,minute);
		cal.set(Calendar.SECOND, 0);	
		AlarmManager alarmM=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
		NotificationController notificationController=new NotificationController();
		notificationController.setNotification(context, alarmM,cal.getTimeInMillis(),edtContentNotify.getText().toString());
		int month=cal.get(cal.get(Calendar.MONTH))+1;
		String time=cal.get(Calendar.DAY_OF_MONTH)+"/"+month+"/"+cal.get(Calendar.YEAR)+", "+hour+"h"+minute;
		Notification notification=new Notification(nameChannel, time, edtContentNotify.getText().toString());
		
		NotificationController notiController=new NotificationController(context);
		notiController.saveNotificationDB(notification);
	    }
	});
	
	builder.setNegativeButton(R.string.btnnegative, new OnClickListener()
	{
	    
	    @Override
	    public void onClick(DialogInterface dialog, int which)
	    {
		
	    }
	});
	
	dialog=builder.create();
	dialog.show();
	
	
	/*Dialog dialog=new Dialog(context);
	dialog.setContentView(R.layout.dialogsettime);
	dialog.setTitle("ABC");
	
	dialog.show();*/
    }
  
}

